package com.jiezhuo.controller.jw;


import com.jiezhuo.entity.LoginUser;
import com.jiezhuo.entity.jw.JwVo;
import com.jiezhuo.entity.jzg.JzgVO;
import com.jiezhuo.service.jw.JwUserlistService;
import com.jiezhuo.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.Set;
import java.util.stream.Collectors;

@Api(tags = "教务管理")
@RestController
@RequestMapping("jw-management")
public class JwController {
    @Autowired
    JwUserlistService jwUserlistService;

    @ApiOperation("获得对应学校学期")
    @GetMapping("getSemester")
    public R getSemester(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        String userId=loginUser.getUser().getUserid();
        return jwUserlistService.getSemester(userId);
    }
    @ApiOperation("获得对应学校年级")
    @GetMapping("getGrade")
    public R getGrade(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        String userId=loginUser.getUser().getUserid();
        return jwUserlistService.getGrade(userId);
    }
    @ApiOperation("获得对应学校年级学科")
    @GetMapping("getSubject")
    public R getSubject(JwVo jwVo){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        String userId=loginUser.getUser().getUserid();
        jwVo.setUserId(userId);
        R userPermissionInfo = jwUserlistService.getUserPermissionInfo(userId);
        if(ObjectUtils.isEmpty(jwVo.getGzXqdm())){
            jwVo.setGzXqdm(userPermissionInfo.getData().get("schools").toString());
        }else {
            Set<String> collect = Arrays.stream(userPermissionInfo.getData().get("schools").toString().split(",")).collect(Collectors.toSet());
            boolean contains = collect.contains("'" + jwVo.getGzXqdm() + "'");
            if (!contains){
                jwVo.setGzXqdm("'not'");
            }
        }
        return jwUserlistService.getSubject(jwVo);
    }

    @ApiOperation("获得对应学校学科")
    @GetMapping("getSubjectAll")
    public R getSubjectAll(JwVo jwVo){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        String userId=loginUser.getUser().getUserid();
        jwVo.setUserId(userId);
        R userPermissionInfo = jwUserlistService.getUserPermissionInfo(userId);
        if(ObjectUtils.isEmpty(jwVo.getGzXqdm())){
            jwVo.setGzXqdm(userPermissionInfo.getData().get("schools").toString());
        }else {
            Set<String> collect = Arrays.stream(userPermissionInfo.getData().get("schools").toString().split(",")).collect(Collectors.toSet());
            boolean contains = collect.contains("'" + jwVo.getGzXqdm() + "'");
            if (!contains){
                jwVo.setGzXqdm("'not'");
            }
        }
        return jwUserlistService.getSubjectAll(jwVo);
    }

    @ApiOperation("获得对应学校教师教学学科")
    @GetMapping("getTeacherSubject")
    public R getTeacherSubject(JwVo jwVo){

        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        String userId=loginUser.getUser().getUserid();
        jwVo.setUserId(userId);
        R userPermissionInfo = jwUserlistService.getUserPermissionInfo(userId);
        if(ObjectUtils.isEmpty(jwVo.getGzXqdm())){
            jwVo.setGzXqdm(userPermissionInfo.getData().get("schools").toString());
        }else {
            Set<String> collect = Arrays.stream(userPermissionInfo.getData().get("schools").toString().split(",")).collect(Collectors.toSet());
            boolean contains = collect.contains("'" + jwVo.getGzXqdm() + "'");
            if (!contains){
                jwVo.setGzXqdm("'not'");
            }
        }


        return jwUserlistService.getTeacherSubject(jwVo);
    }
    @ApiOperation("获得行政管理页面")
    @GetMapping("getXzGl")
    public R getXzGl(JwVo jwVo){

        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        String userId=loginUser.getUser().getUserid();
        jwVo.setUserId(userId);
        R userPermissionInfo = jwUserlistService.getUserPermissionInfo(userId);
        if(ObjectUtils.isEmpty(jwVo.getGzXqdm())){
            jwVo.setGzXqdm(userPermissionInfo.getData().get("schools").toString());
        }else {
            Set<String> collect = Arrays.stream(userPermissionInfo.getData().get("schools").toString().split(",")).collect(Collectors.toSet());
            boolean contains = collect.contains("'" + jwVo.getGzXqdm() + "'");
            if (!contains){
                jwVo.setGzXqdm("'not'");
            }
        }

        return jwUserlistService.getXzGl(jwVo);
    }
    @ApiOperation("获得行政班学科配置页面")
    @GetMapping("getXzXkPz")
    public R getXzXkPz(JwVo jwVo){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        String userId=loginUser.getUser().getUserid();
        jwVo.setUserId(userId);
        R userPermissionInfo = jwUserlistService.getUserPermissionInfo(userId);
        if(ObjectUtils.isEmpty(jwVo.getGzXqdm())){
            jwVo.setGzXqdm(userPermissionInfo.getData().get("schools").toString());
        }else {
            Set<String> collect = Arrays.stream(userPermissionInfo.getData().get("schools").toString().split(",")).collect(Collectors.toSet());
            boolean contains = collect.contains("'" + jwVo.getGzXqdm() + "'");
            if (!contains){
                jwVo.setGzXqdm("'not'");
            }
        }

        return jwUserlistService.getXzXkPz(jwVo);
    }
    @ApiOperation("获得走班学科配置页面")
    @GetMapping("getXzXkZB")
    public R getXzXkZB(JwVo jwVo){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        String userId=loginUser.getUser().getUserid();
        jwVo.setUserId(userId);
        R userPermissionInfo = jwUserlistService.getUserPermissionInfo(userId);
        if(ObjectUtils.isEmpty(jwVo.getGzXqdm())){
            jwVo.setGzXqdm(userPermissionInfo.getData().get("schools").toString());
        }else {
            Set<String> collect = Arrays.stream(userPermissionInfo.getData().get("schools").toString().split(",")).collect(Collectors.toSet());
            boolean contains = collect.contains("'" + jwVo.getGzXqdm() + "'");
            if (!contains){
                jwVo.setGzXqdm("'not'");
            }
        }

        return jwUserlistService.getXzXkZB(jwVo);
    }
    @ApiOperation("获得走班班级数")
    @GetMapping("getXzXkZBNum")
    public R getXzXkZBNum(JwVo jwVo){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        String userId=loginUser.getUser().getUserid();
        jwVo.setUserId(userId);
        R userPermissionInfo = jwUserlistService.getUserPermissionInfo(userId);
        if(ObjectUtils.isEmpty(jwVo.getGzXqdm())){
            jwVo.setGzXqdm(userPermissionInfo.getData().get("schools").toString());
        }else {
            Set<String> collect = Arrays.stream(userPermissionInfo.getData().get("schools").toString().split(",")).collect(Collectors.toSet());
            boolean contains = collect.contains("'" + jwVo.getGzXqdm() + "'");
            if (!contains){
                jwVo.setGzXqdm("'not'");
            }
        }

        return jwUserlistService.getXzXkZBNum(jwVo);
    }

    @ApiOperation("获得系统用户")
    @GetMapping("getJwUser")
    public R getJwUser(JwVo jwVo){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        String userId=loginUser.getUser().getUserid();
        jwVo.setUserId(userId);
        R userPermissionInfo = jwUserlistService.getUserPermissionInfo(userId);
        if(ObjectUtils.isEmpty(jwVo.getGzXqdm())){
            jwVo.setGzXqdm(userPermissionInfo.getData().get("schools").toString());
        }else {
            Set<String> collect = Arrays.stream(userPermissionInfo.getData().get("schools").toString().split(",")).collect(Collectors.toSet());
            boolean contains = collect.contains("'" + jwVo.getGzXqdm() + "'");
            if (!contains){
                jwVo.setGzXqdm("'not'");
            }else {
                jwVo.setGzXqdm("'"+jwVo.getGzXqdm()+"'");
            }
        }

        return jwUserlistService.getJwUser(jwVo);
    }
}
